<template>
  <div>
    <el-cascader :props="wmsProp" v-model="warehouseArr" clearable filterable></el-cascader>

  </div>
</template>

<script>
import { getWarehouseSimple } from "@/api/wms/warehouse"
import { getWarehouseAreaSimple } from '@/api/wms/warehouseArea'
import { getWarehouseLocationSimple } from '@/api/wms/warehouseLocation'

export default {
  name: "SelectWarehouse",
  props: {
    value: {
      required: true
    }
  },
  data() {
    return {
      wmsProp: {
        value: 'index',
        label: 'name',
        lazy: true,
        lazyLoad: this.loadNode,
      },
      warehouseArr: this.value
    }
  },
  watch: {
    value(newVal) {
      this.warehouseArr = newVal;
    },
    warehouseArr(newVal, oldVal){
      this.$emit("input", this.warehouseArr)
    }
  },
  methods: {
    loadNode(node, resolve) {
      // 首次加载时 node为{root:true,level:0}
      // node 节点数据  获取node的level字段的值
      const { level, data } = node;
      //下一层节点
      let nodes = [];
      if (level == 0) {
        getWarehouseSimple().then(response => {
          nodes = response.data;
          if (nodes != undefined && nodes != null && nodes.length > 0) {
            nodes.map(item=> {
              item.leaf = false
              if (item.openArea == 1) {
                item.leaf = true
              }
            })
          }
          resolve(nodes)
        })
      }else if (level == 1) {
        getWarehouseAreaSimple(data.id).then(response => {
          nodes = response.data;
          if (nodes != undefined && nodes != null && nodes.length > 0) {
            nodes.map(item=> {
              item.leaf = false
              if (item.openLocation == 1) {
                item.leaf = true
              }
            })
          }
          resolve(nodes)
        })
      }else if (level == 2) {
        getWarehouseLocationSimple(data.id).then(response => {
          nodes = response.data;
          if (nodes != undefined && nodes != null && nodes.length > 0) {
            nodes.map(item=> {
              item.leaf = true
            })
          }
          resolve(nodes)
        })
      }else {
        //如果没有子节点就不发起请求，直接渲染，也避免了点击叶子节点仍然有加载动画的问题
        resolve(nodes);
      }
    },
  }
};
</script>
<style>

</style>

